package designpatterns.adapter.adaobject.code;

import java.util.Objects;

/**
 * 对象适配器（使用聚合来优化继承）
 */
public class VoltageAdapter implements IVoltage5V {
    // 聚合
    private Voltage220V voltage220V;

    public VoltageAdapter(Voltage220V voltage220V) {
        this.voltage220V = voltage220V;
    }

    @Override
    public Integer outPut5V() {
        if (Objects.isNull(voltage220V)) {
            throw new RuntimeException("被适配对象不能为空！");
        }
        // 适配操作
        int voltage_5 = voltage220V.outPut() / 44;
        System.out.println("适配器输出电压" +voltage_5+"伏");
        return voltage_5;
    }
}
